package com.cn.mobileproject.db.dao.impl;

import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.cn.mobileproject.bean.Enterprise;
import com.cn.mobileproject.db.Query;
import com.cn.mobileproject.db.RowMapper;
import com.cn.mobileproject.db.SqliteTemplate;
import com.cn.mobileproject.db.UDatabase;
import com.cn.mobileproject.db.dao.EnterpriseDao;
import com.cn.mobileproject.db.table.EnterpriseTable;

public class EnterpriseDaoImpl implements EnterpriseDao {
	
	private SqliteTemplate sqliteTemplate;
	
	public EnterpriseDaoImpl(){
		sqliteTemplate = new SqliteTemplate();
	}

	@Override
	public void save(Enterprise enterprise) {

	}

	@Override
	public void inserts(List<Enterprise> list) {
		if(list==null||list.size()==0) return ;
		try{
			SQLiteDatabase db = UDatabase.getDb(true);
			db.beginTransaction();
			for(Enterprise ou: list){
				db.replace(EnterpriseTable.TABLE_NAME, null, enterpriseToValues(ou));
			}
			db.setTransactionSuccessful();
			db.endTransaction();
		}catch(Exception e){
		}
	}
	
	@Override
	public void delete(String projectid){
		Query query = new Query();
		query.from(EnterpriseTable.TABLE_NAME).where(EnterpriseTable.FIELD_PROJECTID+ "=?", projectid);
		sqliteTemplate.delete(query);
	}

	@Override
	public List<Enterprise> search(String projectid) {
		Query query = new Query();
		query.from(EnterpriseTable.TABLE_NAME).where(EnterpriseTable.FIELD_PROJECTID+ "=?", projectid);
		return sqliteTemplate.queryForList(query,new EnterpriseMapper());
	}
	
	private ContentValues enterpriseToValues(Enterprise enterprise) {
		ContentValues v = new ContentValues();
		v.put(EnterpriseTable.FIELD_CONTRACTCODE, enterprise.contractcode);
		v.put(EnterpriseTable.FIELD_CONTRACTNAME, enterprise.contractname);
		v.put(EnterpriseTable.FIELD_CONTRACTTYPE, enterprise.contracttype);
		v.put(EnterpriseTable.FIELD_DISPLAYVALUE, enterprise.displayvalue);
		v.put(EnterpriseTable.FIELD_ENTERPRISEID, enterprise.enterpriseid);
		v.put(EnterpriseTable.FIELD_ENTERPRISENAME, enterprise.enterprisename);
		v.put(EnterpriseTable.FIELD_PROJECTID, enterprise.projectid);
		return v;
	}

	private static final class EnterpriseMapper implements
			RowMapper<Enterprise> {
		public Enterprise mapRow(Cursor cursor, int rowNum) {
			Enterprise enterprise = new Enterprise();
			if (cursor != null && cursor.getCount() > 0) {
				enterprise.contractcode=cursor.getString(cursor.getColumnIndex(EnterpriseTable.FIELD_CONTRACTCODE));
				enterprise.contractname=cursor.getString(cursor.getColumnIndex(EnterpriseTable.FIELD_CONTRACTNAME));
				enterprise.contracttype=cursor.getString(cursor.getColumnIndex(EnterpriseTable.FIELD_CONTRACTTYPE));
				enterprise.displayvalue=cursor.getString(cursor.getColumnIndex(EnterpriseTable.FIELD_DISPLAYVALUE));
				enterprise.enterpriseid=cursor.getString(cursor.getColumnIndex(EnterpriseTable.FIELD_ENTERPRISEID));
				enterprise.enterprisename=cursor.getString(cursor.getColumnIndex(EnterpriseTable.FIELD_ENTERPRISENAME));
				enterprise.projectid=cursor.getString(cursor.getColumnIndex(EnterpriseTable.FIELD_PROJECTID));
			}
			return enterprise;
		}
	}

}
